package main

//https://leetcode-cn.com/problems/where-will-the-ball-fall/
import "fmt"

func findBall(grid [][]int) []int {
	rowL, colL := len(grid), len(grid[0])
	res := make([]int, colL)
	fill := func(x int) int {
		r, c := 0, x
		for r < rowL {
			ne := c + grid[r][c]
			//跑出边界
			if ne < 0 || ne >= colL {
				return -1
			}
			//卡V的情况
			if grid[r][c] != grid[r][ne] {
				return -1
			}
			r++
			c = ne
		}
		return c
	}

	for i := 0; i < colL; i++ {
		res[i] = fill(i)
	}
	return res
}

func main() {
	fmt.Println(findBall([][]int{{1, 1, 1, -1, -1}, {1, 1, 1, -1, -1}, {-1, -1, -1, 1, 1}, {1, 1, 1, 1, -1}, {-1, -1, -1, -1, -1}}))
	fmt.Println(findBall([][]int{{-1}}))
	fmt.Println(findBall([][]int{{1, 1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1, -1}, {1, 1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1, -1}}))
}
